package com.wlhse.service.impl;

import com.github.pagehelper.PageHelper;
import com.wlhse.dao.*;
import com.wlhse.dto.EmployeeManagementDto;
import com.wlhse.dto.getDto.EmloyeeGetDto;
import com.wlhse.dto.inDto.CheckRecordInDto;
import com.wlhse.dto.inDto.CheckRecordQueryInDto;
import com.wlhse.dto.inDto.ProblemImportInDto1;
import com.wlhse.dto.outDto.CheckListOutDto2;
import com.wlhse.dto.outDto.CheckRecordRectRecord;
import com.wlhse.entity.CheckListPojo_cys;
import com.wlhse.entity.CheckRecordPojo;
import com.wlhse.entity.CheckRecordPojo_cys;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.ProblemImportService1;
import com.wlhse.service.ProblemImportService_cys;
import com.wlhse.util.HashUtil;
import com.wlhse.util.R;
import com.wlhse.util.TreeUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class ProblemImportService_cysImpl implements ProblemImportService_cys {
    @Value("${RESOURCES_URL}")
    private String url;
    @Resource
    private ProblemImportDao_cys problemImportDao_cys;
    @Resource
    private CheckListItemDao checkListItemDao;
    @Resource
    private CheckRecordDao checkRecordDao;
    @Resource
    private EmployeeManagementDao employeeManagementDao;
    @Resource
    private CompanyDao companyDao;
    @Resource
    private EmployeeManagementDao employeeDao;
    @Resource
    private TreeUtil treeUtil;

//    @Override
//    public String listGist(int checklistitemID) {
//        return NR.r(problemImportDao_cys.queryGist(checklistitemID));
//    }

    public CheckRecordPojo_cys initNewCheckRecordPojo(CheckRecordPojo_cys checkRecordPojo){
        if(checkRecordPojo.getCheckStaffID()!=null && checkRecordPojo.getCheckStaffID()!=0){
            EmployeeManagementDto checkStaff = employeeManagementDao.queryById(checkRecordPojo.getCheckStaffID());
            checkRecordPojo.setCheckStaffName(checkStaff.getName());
        }

        if(checkRecordPojo.getVerifyPersonEmpID()!=null && checkRecordPojo.getVerifyPersonEmpID()!=0){
            EmployeeManagementDto verifyPerson = employeeManagementDao.queryById(checkRecordPojo.getVerifyPersonEmpID());
            checkRecordPojo.setVerifyPersonName(verifyPerson.getName());
            if(!checkRecordPojo.getVerifyPersonEmpID().equals(checkRecordPojo.getCheckStaffID())){
                checkRecordPojo.setStatus("未审核");
            }
        }
        if(checkRecordPojo.getCompanyCode()!=null && !checkRecordPojo.getCompanyCode().isEmpty()){
            checkRecordPojo.setCompanyName(companyDao.queryByCompanyCode(checkRecordPojo.getCompanyCode()));
            checkRecordPojo.setParentCompanyName(companyDao.getParentCompanyByCode(checkRecordPojo.getCompanyCode()));
            checkRecordPojo.setBasicCompanyName(companyDao.getBasicCompanyByCode(checkRecordPojo.getCompanyCode()));
        }
        if(checkRecordPojo.getResponsePersonID()!=null && checkRecordPojo.getResponsePersonID()!=0){
            EmployeeManagementDto responsePerson = employeeManagementDao.queryById(checkRecordPojo.getResponsePersonID());
            checkRecordPojo.setResponsePersonName(responsePerson.getName());
        }
        if(checkRecordPojo.getSupervisorID()!=null && checkRecordPojo.getSupervisorID()!=0){
            EmployeeManagementDto supervisor = employeeManagementDao.queryById(checkRecordPojo.getSupervisorID());
            checkRecordPojo.setSupervisorName(supervisor.getName());
        }
        if(checkRecordPojo.getCheckListID()!=null && checkRecordPojo.getCheckListID()!=0){
            CheckListPojo_cys checkListPojo = problemImportDao_cys.queryCheckListByID(checkRecordPojo.getCheckListID());
//            checkRecordPojo.setCheckType(checkListPojo.getCheckListType());
            checkRecordPojo.setCheckListName(checkListPojo.getCheckListName());
        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmssSSS");
        String checkRecodeNo=sdf.format(date);
        checkRecordPojo.setCheckRecordNo(checkRecodeNo);
        return checkRecordPojo;
    }

    public CheckRecordPojo_cys initNewCheckRecordPojo2(CheckRecordPojo_cys checkRecordPojo){
        System.out.println(checkRecordPojo);
        if(checkRecordPojo.getCheckStaffID()!=null && checkRecordPojo.getCheckStaffID()!=0){
            EmployeeManagementDto checkStaff = employeeManagementDao.queryById(checkRecordPojo.getCheckStaffID());
            checkRecordPojo.setCheckStaffName(checkStaff.getName());
            checkRecordPojo.setVerifyPersonEmpID(checkRecordPojo.getCheckStaffID());
            checkRecordPojo.setVerifyPersonName(checkStaff.getName());
        }
        if(checkRecordPojo.getCompanyCode()!=null && !checkRecordPojo.getCompanyCode().isEmpty()){
            if(checkRecordPojo.getCompanyCode().length()==2||checkRecordPojo.getCompanyCode().length()==4){
                checkRecordPojo.setParentCompanyName(companyDao.getParentCompanyByCode(checkRecordPojo.getCompanyCode()));
            }else{
                checkRecordPojo.setCompanyName(companyDao.queryByCompanyCode(checkRecordPojo.getCompanyCode()));
                checkRecordPojo.setParentCompanyName(companyDao.getParentCompanyByCode(checkRecordPojo.getCompanyCode()));
                checkRecordPojo.setBasicCompanyName(companyDao.getBasicCompanyByCode(checkRecordPojo.getCompanyCode()));
            }
        }
        if(checkRecordPojo.getResponsePersonID()!=null && checkRecordPojo.getResponsePersonID()!=0){
            EmployeeManagementDto responsePerson = employeeManagementDao.queryById(checkRecordPojo.getResponsePersonID());
            checkRecordPojo.setResponsePersonName(responsePerson.getName());
        }
        if(checkRecordPojo.getSupervisorID()!=null && checkRecordPojo.getSupervisorID()!=0){
            EmployeeManagementDto supervisor = employeeManagementDao.queryById(checkRecordPojo.getSupervisorID());
            checkRecordPojo.setSupervisorName(supervisor.getName());
        }
        if(checkRecordPojo.getCheckListID()!=null && checkRecordPojo.getCheckListID()!=0){
            CheckListPojo_cys checkListPojo = problemImportDao_cys.queryCheckListByID(checkRecordPojo.getCheckListID());
            if(null!=checkListPojo){
                if(null!=checkListPojo.getCheckListType())
                    checkRecordPojo.setCheckType(checkListPojo.getCheckListType());

                checkRecordPojo.setCheckListName(checkListPojo.getCheckListName());
            }

        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmssSSS");
        String checkRecodeNo=sdf.format(date);
        checkRecordPojo.setCheckRecordNo(checkRecodeNo);
        return checkRecordPojo;
    }

    @Override
    public String listHseFactor(int checklistitemID) {
        return NR.r(problemImportDao_cys.queryHseFactor(checklistitemID));
    }

    @Override
    public String listDictRectification() {
        return NR.r(problemImportDao_cys.queryDictRectification());
    }

    @Override
    public String listDictRectEffect() {
        return NR.r(problemImportDao_cys.queryDictRectEffect());
    }

    @Override
    public String listDictLostScore() {
        return NR.r(problemImportDao_cys.queryDictLostScore());
    }

    @Override
    public String listDictPenalty() {
        return NR.r(problemImportDao_cys.queryDictPenalty());
    }

    private void setDateChangQin(CheckRecordInDto checkRecordInDto) {
        if (StringUtils.isNotBlank(checkRecordInDto.getCheckDate1()) && StringUtils.isBlank(checkRecordInDto.getCheckDate2())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            checkRecordInDto.setCheckDate2(df.format(new Date()));// new Date()为获取当前系统时间
        }
    }

    @Override
    public R newInsertProblemImport(CheckRecordPojo_cys checkRecordPojo, int userId) {
        CheckRecordPojo_cys newcheckRecordPojo = initNewCheckRecordPojo(checkRecordPojo);
        int result = problemImportDao_cys.addNewCheckRecord(newcheckRecordPojo);
        return R.ok();

    }
    @Override
    public String getAllEmployeeDto(EmloyeeGetDto dto, int userId){
        List<EmployeeManagementDto> allEmployeeDto = employeeDao.getAllEmployeeDto(dto.getName(), dto.getCompanyCode());
        return NR.r(allEmployeeDto);
    }

    @Override
    public R updateProblemImport(CheckRecordPojo_cys checkRecordPojo, int userId) {
        System.out.println("33333333333");
        CheckRecordPojo_cys newcheckRecordPojo = initNewCheckRecordPojo2(checkRecordPojo);
        System.out.println("4444444444");
        problemImportDao_cys.updateCheckRecord(newcheckRecordPojo);
        System.out.println("结果");
        return R.ok();

    }

    @Override
    public String queryNoveriy(CheckRecordInDto checkRecordInDto) {
        checkRecordInDto.setUrl(url);
        setDateChangQin(checkRecordInDto);
        int length = checkRecordDao.getMaxLengthCompanyCode();
        int total=0;
        List<CheckRecordPojo> list=new ArrayList<>();
        if (checkRecordInDto.getType().equals("all"))
            return NR.r(problemImportDao_cys.getCheckRecordByCondition(checkRecordInDto));
        //分页查询
        total = problemImportDao_cys.getchildCrByConditionCount(checkRecordInDto);
        PageHelper.startPage(checkRecordInDto.getPageIdx(), checkRecordInDto.getPageSize());
        list = problemImportDao_cys.getchildCrByCondition(checkRecordInDto);
        return NR.r(list, total, checkRecordInDto.getPageIdx());
    }

    @Override
    public String queryCheckListItem(){
        return NR.r(checkListItemDao.queryCheckListItem());
    }

    @Override
    public String getCheckListItemByid(int id){
        return NR.r(checkListItemDao.queryCheckListItemByid(id));
    }

    @Override
    public String getCheckList_Item(){
        List<CheckListOutDto2> checkList = checkListItemDao.queryCheckListCodeName();
        List<CheckListOutDto2> checkListitem = checkListItemDao.queryCheckListItemCodeName();
        checkList.addAll(checkListitem);
        return NR.r(CodeDict.CODE_MESSAGE_DATA, 0, 0, treeUtil.GetCheckList_Item(checkList), null, 0, 0);
    }

}
